﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;

using DQYT.HTDZ.ISinglewellEvaluationService;
using DQYT.HTDZ.Model;
using Maticsoft.DBUtility;


namespace DQYT.HTDZ.SinglewellEvaluationService
{
    /// <summary>
    /// 所有的方案都使用该类
    /// </summary>
    public class StandardSchemeService : ISchemeService
    {

        /// <summary>
        /// key为评价项ID，value为评价项得分
        /// </summary>
        /// <param name="ps"></param>
        /// <returns></returns>
        public EvaluationModel SchemeEvaluate(IEnumerable<KeyValuePair<string, string>> ps)
        {

            EvaluationModel model = new EvaluationModel();

            // 评价项ID
            string evaluationValue = string.Empty;

            // 方案ID
            string schemeID =ps.Where(p=>p.Key.ToLower().Equals("schemeid")).Select(p=>p.Value).First();
            if(string.IsNullOrWhiteSpace(schemeID)) return null;
            decimal schemeid = decimal.Parse(schemeID);

            DataTable schemeEvaluationTable = DbHelperOra.Query_Table("select A.scheme_id, A.EVAL_ID, A.qz, B.scheme_name from TB_SCHEME_EVAL A join TB_Scheme B on A.scheme_id = B.scheme_id where A.scheme_id = '" + schemeid + "'");
            if(schemeEvaluationTable == null || schemeEvaluationTable.Rows.Count == 0)
                return null;

            double sumQz = 0;
            foreach (DataRow row in schemeEvaluationTable.Rows)
            {
                // 评价项权重
                double qz = float.Parse(row["qz"].ToString());
                // 单个评价项得分
                evaluationValue = ps.Where(p => p.Key.Equals(row["EVAL_ID"].ToString())).Select(p => p.Value).First();
                model.EvaluationValue += qz * double.Parse(evaluationValue);
                sumQz += qz;
            }
            model.EvaluationValue = Math.Round((model.EvaluationValue) / sumQz,0);
            //保存方案名称
            model.EvaluationName = schemeEvaluationTable.Rows[0]["scheme_name"].ToString();

            //保存方案描述
            model.EvaluationDesc = "从方案得分定义表里读取对应分数的描述";

            return model;
        }
    }
}
